package com.c.yinyuezhushou.Adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.MyTool.DateChange;
import com.c.yinyuezhushou.Fragment.CoverFragment;
import com.c.yinyuezhushou.Fragment.LyricsFragment;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Ui.PlayerActivity;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class LyricRecAdapter extends RecyclerView.Adapter{

    private String[] strings;
    private List<Long> list = new ArrayList<Long>();
    private PlayerActivity activity;
    private LyricsFragment lyricsFragment;
    private boolean new1 = false;

    public boolean isNew1() {
        return new1;
    }

    public List<Long> getList() {
        return list;
    }


    public LyricRecAdapter(String s, LyricsFragment lyricsFragment){
        this.lyricsFragment = lyricsFragment;
        if (!s.isEmpty()){
            String[] strings = StringUtils.split(s,"\n");
            this.strings = new String[strings.length];
            int i = 0;
            for (String s1:strings){
                long hm = DateChange.getmillisecond(StringUtils.substring(s1,1,11));
                list.add(Long.valueOf(hm));
                String[] strings1 = StringUtils.split(s1,']');
                if (strings1.length>1){
                    this.strings[i] = strings1[1];
                }else {
                    this.strings[i] = "";
                }
                i++;
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.ilr_text);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lyric_recycler,parent,false);
        activity = (PlayerActivity) parent.getContext();
        LyricRecAdapter.ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.setIsRecyclable(false);
        return viewHolder;
    }

    public void getcz(int cz){
            List<Long> list1 = new ArrayList<Long>();
            String[] s1 = new String[2 * cz + strings.length];
            int j = cz;
            for (String s : strings) {
                s1[j] = s;
                j++;
            }
            strings = s1;
            for (int i = 0; i < cz; i++) {
                list1.add(Long.valueOf(0));
            }
            List<Long> list2 = new ArrayList<Long>();
            for (int i = 0; i < cz; i++) {
                list2.add(Long.valueOf(lyricsFragment.getDuration()));
            }
            list1.addAll(list);
            list1.addAll(list2);
            list = list1;
            LyricRecAdapter.this.notifyDataSetChanged();
            Log.d("test1", "4");
            new1 = true;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.textView.setText(strings[position]);
        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Switch();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void Switch(){
        CoverFragment coverFragment = activity.getCoverFragment();
        FragmentTransaction ft=activity.getSupportFragmentManager().beginTransaction();

        ft.hide(lyricsFragment);
        ft.show(coverFragment);

        ft.commit();
    }

}
